import {combineReducers} from "redux";
import {
    FETCH_USER_REQUEST,
    FETCH_USER_RECEIVE,
    TOGGLE_MENU,
    SELECT_MENU_ITEM,
    FETCH_ACTION_REQUEST,
    FETCH_ACTION_RECEIVE,
    INVALIDATE_SUBJECT,
    FETCH_ACTION_REMOVED_RECEIVE,
    FETCH_USER_CLASS_SELECT,
    FETCH_USER_CLASS_SELECT_ALL,
    SET_FORM_ITEM,
    ENABLE_DELETE_BTN,
    SELECT_ALL_ITEM,
    SELECT_ITEM,
    OPEN_MODAL,
    CLOSE_MODAL,
    UPDATE_CURRENT_NAV_ITEM,
    CLIENT_INFO_SET
} from "./actionType";
import {SUBJECT, PAGE_SUBJECT} from "./subject";
import {Url, md5Url} from "../libs/Url";


function toggle(state = {}, action) {
    switch (action.type) {
        case TOGGLE_MENU:
            return Object.assign({}, state, {collapse: !state.collapse});
        default:
            return {collapse: false};
    }
}
/***
 * 选中菜单项
 * @param state
 * @param action
 * @returns {*}
 */
function selected(state = {}, action) {
    switch (action.type) {
        case SELECT_MENU_ITEM:
            return Object.assign({}, state, {menuItem: !state.selected});
        default:
            return {menuItem: ''};
    }
}

function user_login(state = {
    user: {class: []},
    isFetching: false
}, action) {
    switch (action.type) {
        case FETCH_USER_REQUEST:
            return Object.assign({}, state, {isFetching: true});
        case FETCH_USER_RECEIVE:
            return Object.assign({}, state, {isFetching: false, user: action.user});
        case FETCH_USER_CLASS_SELECT:
            return Object.assign({}, state, {
                user: {
                    class: [
                        ...state.user.class.slice(0, action.index),
                        _.extend({}, state.user.class[action.index], {
                            checked: !state.user.class[action.index].checked
                        }),
                        ...state.user.class.slice(action.index + 1)
                    ]
                }
            });
        case FETCH_USER_CLASS_SELECT_ALL:
            return Object.assign({}, state, {
                user: {
                    class: state.user.class.map(item=> {
                        item.checked = true;
                        return item;
                    })
                }
            });
        default:
            return state;
    }
}

///-------------------------
//全选，
export function pageBySubject(state = (item=> {
    {
        var tmp = {};
        for (var pro in PAGE_SUBJECT) {
            tmp[PAGE_SUBJECT[pro]] = {
                current: 0
            }
        }
        return tmp;
    }
})(), action) {
    switch (action.type) {
        case UPDATE_CURRENT_NAV_ITEM:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: {
                        current: action.current
                    }
                }
            );
        case ENABLE_DELETE_BTN:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: {
                        enable_delete_btn: action.all ? action.num :
                            (state[action.subject].enable_delete_btn + action.num)
                    }
                }
            );
        case OPEN_MODAL:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: {
                        open: true
                    }
                }
            );
        case CLOSE_MODAL:
            return Object.assign({},
                ...state,
                {
                    [action.subject]: {
                        open: false
                    }
                }
            );
        default:
            return state;
    }
}

///-------------------------
/***
 * subject request
 * @param state
 * @param action
 * @returns {*}
 */
function subject_request(state = {
    isFetching: false,
    didInvalidate: false,
    data: {},
    page: 1,
    pages: 1,
    query: {}//查询参数
}, action) {
    switch (action.type) {
        case INVALIDATE_SUBJECT:
            return Object.assign({}, state, {
                didInvalidate: true
            });
        case FETCH_ACTION_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case FETCH_ACTION_RECEIVE:
            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                lastUpdated: action.receivedAt,
                ...action.json,
                query: action.query
            });
        case FETCH_ACTION_REMOVED_RECEIVE:
            const tm = [];
            if ((action.ids.indexOf(',') > -1) || (action.index == undefined)) {
                state.list.map((item)=> {
                    !item.checked && tm.push(item);
                });
            } else {
                return Object.assign({}, state, {
                    list: [
                        ...state.list.slice(0, action.index),
                        ...state.list.slice(action.index + 1)
                    ]
                });
            }
            return Object.assign({}, state, {
                list: tm
            });
        default:
            return state
    }
}
/***
 * fetch by subject
 * @param state
 * @param action
 * @returns {*}
 */
function fetchBySubject(state = (()=> {
    var tmp = {};
    for (var pro in md5Url) {
        tmp[md5Url[pro]] = {};
    }
    return tmp;
})(), action) {
    switch (action.type) {
        case INVALIDATE_SUBJECT: // invalidate subject
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            });
        case FETCH_ACTION_RECEIVE: // fetch receive subject
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            });
        case FETCH_ACTION_REQUEST: //fetch request subject
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            });
        case FETCH_ACTION_REMOVED_RECEIVE:
            return Object.assign({}, state, {
                [action.subject]: subject_request(state[action.subject], action)
            })
        case SELECT_ALL_ITEM:
            return Object.assign({}, state, {
                [action.subject]: {
                    list: state[action.subject].list.map((item)=> {
                        item.checked = action.flag;
                        return item;
                    })
                }
            });
        case SELECT_ITEM:
            return Object.assign({}, state, {
                [action.subject]: {
                    list: [
                        ...state[action.subject].list.slice(0, action.index),
                        _.extend({}, state[action.subject].list[action.index], {
                            checked: action.checked
                        }),
                        ...state[action.subject].list.slice(action.index + 1)
                    ]
                }
            });
        default:
            return state;
    }
}
function clientInfo(state = {}, action) {
    switch (action.type) {
        case CLIENT_INFO_SET:
            state = action.info;
            return state;
        default:
            return state;
    }
}
const rootReducer = combineReducers({
    fetchBySubject,
    pageBySubject,
    clientInfo
});

export default rootReducer;